package com.example.administrator.capital.mine;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.capital.MainFragment;
import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseMainFragment;
import com.example.administrator.capital.eventbus.EventBase;
import com.example.administrator.capital.login.LoginActivity;
import com.example.administrator.capital.mine.second.BindCardFragment;
import com.example.administrator.capital.model.bean.AccountBean;
import com.example.administrator.capital.model.bean.GetMoneyBean;
import com.example.administrator.capital.model.bean.OneMemBean;
import com.example.administrator.capital.presenter.MinePresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.JsonUtils;
import com.example.administrator.capital.utils.PreferenceManager;
import com.example.administrator.capital.utils.ToastUtil;
import com.lzy.okgo.model.Response;
import com.sunfusheng.glideimageview.GlideImageView;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import eventbusactivityscope.EventBusActivityScope;
/**
 * A simple {@link Fragment} subclass.
 * 我的
 */
public class MineFragment extends BaseMainFragment {
    @BindView(R.id.news)
    ImageView mNews;
    @BindView(R.id.pay)
    TextView mPay;
    Unbinder unbinder;
    @BindView(R.id.get_money)
    TextView mGetMoney;
    @BindView(R.id.platform_information)
    TextView mPlatformInformation;
    @BindView(R.id.investment_record)
    TextView mInvestmentRecord;
    @BindView(R.id.recharge_record)
    TextView mRechargeRecord;
    @BindView(R.id.get_money_record)
    TextView mGetMoneyRecord;
    @BindView(R.id.account_setting)
    TextView mAccountSetting;
    @BindView(R.id.help_center)
    TextView mHelpCenter;
    @BindView(R.id.online_service)
    TextView mOnlineService;
    @BindView(R.id.user_pic)
    GlideImageView mUserPic;
    @BindView(R.id.username)
    TextView mUsername;
    @BindView(R.id.amount)
    TextView mAmount;
    @BindView(R.id.freeze)
    TextView mFreeze;
    @BindView(R.id.use_balance)
    TextView mUseBalance;
    private MinePresenter mMinePresenter;
    private boolean isCard;
    private String amount;
    public MineFragment() {
    }
    public static MineFragment newInstance() {
        Bundle args = new Bundle();
        MineFragment fragment = new MineFragment();
        fragment.setArguments(args);
        return fragment; }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view; }
    @Override
    protected void initView(View view) {
        super.initView(view);
        EventBusActivityScope.getDefault(_mActivity).register(this);
        mMinePresenter = new MinePresenter(null); }
    @Override
    protected void setListener() {
        super.setListener();
        mPay.setOnClickListener(this);
        mGetMoney.setOnClickListener(this);
        mNews.setOnClickListener(this);
        mPlatformInformation.setOnClickListener(this);
        mInvestmentRecord.setOnClickListener(this);
        mRechargeRecord.setOnClickListener(this);
        mGetMoneyRecord.setOnClickListener(this);
        mHelpCenter.setOnClickListener(this);
        mAccountSetting.setOnClickListener(this);
        mOnlineService.setOnClickListener(this);
        mUserPic.setOnClickListener(this);
        mUsername.setOnClickListener(this); }
    @Override
    protected void setData() {
        super.setData();

        getData(PreferenceManager.instance().getUseId()); }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.username:
                ((MainFragment) getParentFragment()).startBrotherFragment(AccountSettingFragment.newInstance());
                break;
            case R.id.pay:
                ((MainFragment) getParentFragment()).startBrotherFragment(RechargeFragment.newInstance());
                break;
            case R.id.news:
                ((MainFragment) getParentFragment()).startBrotherFragment(NewsFragment.newInstance());
                break;
            case R.id.get_money:
                tixian();

                break;
            case R.id.platform_information:
                ((MainFragment) getParentFragment()).startBrotherFragment(PlatformInformationFragment.newInstance());
                break;
            case R.id.investment_record:
                ((MainFragment) getParentFragment()).startBrotherFragment(InvestmentRecordFragment.newInstance());
                break;
            case R.id.recharge_record:
                ((MainFragment) getParentFragment()).startBrotherFragment(RechargeRecordFragment.newInstance());
                break;
            case R.id.get_money_record:
                ((MainFragment) getParentFragment()).startBrotherFragment(TouFragment.newInstance());
                break;
            case R.id.account_setting:
                ((MainFragment) getParentFragment()).startBrotherFragment(AccountSettingFragment.newInstance());
                break;
            case R.id.help_center:
                ((MainFragment) getParentFragment()).startBrotherFragment(CenterFragment.newInstance());
                break;
            case R.id.online_service:
                ((MainFragment) getParentFragment()).startBrotherFragment(HelpCenterFragment.newInstance());
                break;
        }
    }
    private void getData(String uid) {
        mMinePresenter.oneMem(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                if (var.equals("请登录")) {
                    mBaseActivity.showActivity(LoginActivity.class,null);
                } else {
                    ToastUtil.showShort(var);

                }
            }
            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }
            @Override
            public void onSuccess(Response<String> data) {
                OneMemBean bean = JsonUtils.parse(data.body(), OneMemBean.class);
                mUsername.setText(bean.getData().getUsername());
            }
            @Override
            public void onFail(Response<String> error) {
            }
        }, uid);
        mMinePresenter.account(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {

            }
            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }
            @Override
            public void onSuccess(Response<String> data) {
                AccountBean bean = JsonUtils.parse(data.body(), AccountBean.class);
                amount = String.valueOf(bean.getData().getAmount() / 100);
                mAmount.setText(bean.getData().getAmount()/100+"元");
                mFreeze.setText(bean.getData().getFreeze()/100+".00");
                mUseBalance.setText(bean.getData().getBalance()/100+".00");
            }
            @Override
            public void onFail(Response<String> error) {
            }
        }, uid);
    }
    private void tixian() {
        mMinePresenter.withdrawals(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }
            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);
            }
            @Override
            public void onSuccess(Response<String> data) {
                GetMoneyBean bean = JsonUtils.parse(data.body(), GetMoneyBean.class);
                if (bean.getData().getData1().size() == 0) {
                    ToastUtil.showShort("请先绑定银行卡");
                    ((MainFragment) getParentFragment()).startBrotherFragment(BindCardFragment.newInstance());
                } else {
                    ((MainFragment) getParentFragment()).startBrotherFragment(GetMoneyFragment.newInstance(amount));
                }
            }
            @Override
            public void onFail(Response<String> error) {
            }
        }, PreferenceManager.instance().getUseId());
    }
    @Subscribe
    public void onTabSelectedEvent(EventBase event) {
        if (event.getName().equals("刷新界面")) {
            getData(PreferenceManager.instance().getUseId());
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
